package com.wlb365.admin.modules.license.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.admin.common.exception.AdminException;
import com.wlb365.admin.common.utils.FileUploadKit;
import com.wlb365.admin.common.utils.HttpServletContextKit;
import com.wlb365.admin.modules.license.entity.License;
import com.wlb365.admin.modules.license.service.LicenseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.wlb365.admin.common.support.controller.BaseController;
import com.wlb365.admin.common.dto.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2020-06-24 09:11:14
 */
@RestController
@RequestMapping("license/server")
public class LicenseController extends BaseController {

    @Autowired
    private LicenseService licenseService;

    @RequiresPermissions("license:server:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("license/license");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("license/license_edit");
        License license;
        if (id == null) {
            license = new License();
        } else {
            license = licenseService.getById(id);
        }
        mv.addObject("editInfo", license);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(License license) {
        Page<License> page = licenseService.listLicensePage(license);
        return R.ok(page);
    }

    @RequiresPermissions("license:server:generate")
    @PostMapping(value = "generate")
    public R add(License license) {
        licenseService.generate(license);
        return R.ok();
    }

    @RequiresPermissions("license:server:downLicense")
    @GetMapping(value = "downLicense/{id}")
    public void downloadLicense(@PathVariable Long id) {
        License license = licenseService.getById(id);
        File file = new File(license.getLicensePath());

        try (InputStream inputStream = new FileInputStream(file)) {
            FileUploadKit.write(HttpServletContextKit.getHttpServletResponse(), inputStream, "text/xml", "license.txt");
        } catch (Exception e) {
            throw new AdminException("文件下载失败！");
        }

    }

    @RequiresPermissions("license:server:downSource")
    @GetMapping(value = "downSource/{id}")
    public void downloadSource(@PathVariable Long id) {
        License license = licenseService.getById(id);
        File file = new File(license.getSourcePath());
        try (InputStream inputStream = new FileInputStream(file)) {
            FileUploadKit.write(HttpServletContextKit.getHttpServletResponse(), inputStream, "text/xml", "source.txt");
        } catch (Exception e) {
            throw new AdminException("文件下载失败！");
        }
    }


    @RequiresPermissions("license:server:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        licenseService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("license:server:edit")
    @PostMapping(value = "edit")
    public R edit(License license) {
        licenseService.updateById(license);
        return R.ok();
    }

    @RequiresPermissions("license:server:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        licenseService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(licenseService.getById(id));
    }

}

